package com.bsoft.gol.hcs.specialist.controller;


import com.alibaba.fastjson.JSONObject;
import com.bsoft.gol.hcs.satisfaction.dao.PatientSatisfactionSurveyDAO;
import com.bsoft.gol.hcs.specialist.entity.DuanLian;
import com.bsoft.gol.hcs.specialist.entity.QueryQuestionRecordsResp;
import com.bsoft.gol.hcs.specialist.service.IDuanLianService;
import com.bsoft.gol.hcs.utils.ShortUrlGenerator;
import hcn.util.CommonUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping(value = "/a/")
public class DurlController  {
    private final static Logger logger = LoggerFactory.getLogger(DurlController.class);
    @Autowired
    private IDuanLianService duanLianService;
    @Autowired
    private PatientSatisfactionSurveyDAO patientSatisfactionSurveyDAO;



    @RequestMapping(value="/{durl}")
    protected void doPost(@PathVariable("durl") String durl, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        logger.info("-------------------------"+durl);
        if (StringUtils.hasText(durl)) {
            long id = ShortUrlGenerator.decimalConvertToNumber(durl, 62);
            DuanLian durlMapping = duanLianService.getDuanLian(id);
            if (null != durlMapping) {
                response.setStatus(302);
                response.sendRedirect(durlMapping.getUrl());
                return;
            }
        }
        response.setStatus(404);
        return;
    }

//    @Override
//    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        this.doPost(request, response);
//    }


    /**
     * 获取某个表单的填写记录
     */
    @RequestMapping(value="/queryQuestionRecords")
    @ResponseBody
    private List<QueryQuestionRecordsResp> queryQuestionRecords(@RequestBody List<String> userInfoIds){

        logger.info("queryQuestionRecords: " + JSONObject.toJSONString(userInfoIds));

        if (CommonUtils.isEmpty(userInfoIds)){
            return null;
        }

        return patientSatisfactionSurveyDAO.queryQuestionRecords(userInfoIds);

    }

}
